package com.compomics.proteocloud.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * Little helper class to inform the user about the total number of spectra.
 * 
 * @author Thilo Muth
 *
 */
public class SpectrumCounter {		

	/**
	 * File array.
	 */
	private File[] files;
	
	/**
	 * Constructor for the SpectrumCounter.
	 * @param files
	 */
	public SpectrumCounter(File[] files) {
		this.files = files;		
	}
	
	/**
	 * Returns the number of spectra.
	 * @return
	 */
	public int getNumberOfSpectra() {
		int spectrumCounter = 0;
		try {
			BufferedReader br = null;
			String line;

			// Iterate over all the files.
			for (int i = 0; i < files.length; i++) {
				br = new BufferedReader(new FileReader(files[i]));
				// Cycle the file.
				while ((line = br.readLine()) != null) {
					line = line.trim();
					if (line.indexOf("END IONS") >= 0) {
						// Increment the spectrumCounter by one.
						spectrumCounter++;
					}
				}
			}

			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return spectrumCounter;
	}

}
